# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lua-pty
_pkgname=lpty
pkgver=1.2.2
_rockver=1
pkgrel=0
pkgdesc="Lua module to control other programs via PTYs."
url="https://www.tset.de/lpty/"
arch="all"
license="MIT"
source="http://www.tset.de/downloads/$_pkgname-$pkgver-$_rockver.tar.gz"
builddir="$srcdir/$_pkgname-$pkgver-$_rockver"

_luaversions="5.1 5.2 5.3"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
done

prepare() {
	default_prepare

	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver"
	done
}

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		cd "$builddir-$lver"
		make CFLAGS="$CFLAGS -fPIC" \
			LUA_INCDIR="$(pkg-config --variable=includedir lua$lver)" \
			LUA_LIBDIR="$(pkg-config --variable=libdir lua$lver)" \
			LUA=lua$lver
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$_pkgname/$pkgver-$_rockver"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	[ "$lver" = 5.1 ] && replaces="$pkgname"

	cd "$builddir-$lver"
	make INST_DIR="$subpkgdir/usr" LUAVERSION=$lver install

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="6251f8394be53ab812bb3483fcd8280c9cf8d63590534864b439f5357a4c11507c3a7d6e03efa11018547435f7eede6ea5896a39f0d9ddaaf3f4f41f7827ba96  lpty-1.2.2-1.tar.gz"
